<?php

class Client extends Controller {

	// Constructor
	function Client()
    {
        parent::Controller();
		
		// load model
		$this->load->model('Df602_model', 'df602', TRUE);
		
		// select table to scaff
		$this->load->scaffolding('realtime');
		
		// load session lib
		$this->load->library('session');
		
		$this->load->helper('form');
		
		
    }


	function removeClient($clientID)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$this->df602->remove_client($clientID);
	}
	
	function removeClientConfirm($clientID)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$clientInfo = $this->df602->get_client_name($clientID);
		
		echo "Do you want to remove ";
		echo "<font color=red>".$clientInfo[0]->name."</font> ";
		echo "from Client List ?";
		echo "<br><br>";
		echo "<lu>";
		echo "<li>";
		
		$homeUrl = "./home";
		$listClientHDUrl = "./home/listAllClientHD";
		$listClientUrl = "./home/listAllClient";
		$removeClientUrl = "./client/removeClient/".$clientID;
		
		echo "<a onClick=\"if(refresher!= '') refresher.stop(); ";
		echo "new Ajax.Updater($('rightContainer'), '".$removeClientUrl."'); ";
		
		echo "refresher = new Ajax.PeriodicalUpdater($('rightContainer'), '".$listClientHDUrl."', {asynchronous:true, frequency:3}); ";
		echo "new Ajax.Updater($('leftContainer'), '".$listClientUrl."'); ";
		echo "new Ajax.Updater($('navigate'), './home/rewriteNavigater', {parameters: { url: '".$listClientHDUrl."', name: 'Home', layer: 0}}); \">";
		echo "Yes</a>";
		
		echo "<a onClick=\"if(refresher!= '') refresher.stop(); refresher = new Ajax.PeriodicalUpdater($('rightContainer'), '".$listClientHDUrl."', {asynchronous:true, frequency:3}); ";
			echo "new Ajax.Updater($('navigate'), './home/rewriteNavigater', {parameters: { url: '".$homeUrl."', name: 'Home', layer: 0}}); \">";
		echo "<li>No</a>";
		echo "</lu>";
	}
	
	function editClientInfo()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$name = $_POST['name'];
		$hostName = $_POST['hostName'];
		$username = $_POST['username'];
		$password = $_POST['password'];
		$clientID = $_POST['clientID'];
		
		$data = array(
               'name' => $name,
               'hostName' => $hostName,
               'username' => $username,
			   'password' => $password
            );

		$this->db->where('id',$clientID);
		if($this->db->update('client', $data) == true) echo "complete"; 
		
		
	}
	
	function editClientForm($clientID)
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$clientInfo = $this->df602->get_client_name($clientID);
		foreach($clientInfo as $row)
		{
			$serverName = array(
	              'name'        => 'serverName',
	              'id'          => 'serverName',
	              'value'       => $row->name,
	              'maxlength'   => '100',
	              'size'        => '25',
				  'class'		=> 'require'
	            );
				
			$hostName = array(
				  'name'        => 'hostName',
	              'id'          => 'hostName',
	              'value'       => $row->hostname,
	              'maxlength'   => '100',
	              'size'        => '25',
				  'class'		=> 'require'
			);
			
			$user = array(
				  'name'        => 'user',
	              'id'          => 'user',
	              'value'       => $row->username,
	              'maxlength'   => '100',
	              'size'        => '25',
				  'class'		=> 'require'
			);
			
			$password = array(
				  'name'        => 'password',
	              'id'          => 'password',
	              'value'       => $row->password,
	              'maxlength'   => '100',
	              'size'        => '25',
				  'class'		=> 'require'
			);
			
		
		}
		
				$attributes = array('id' => 'addServForm', 'onSubmit'=>'return  (checkForm(this))');
		$action = "form/addServer";
		
		echo form_open($action, $attributes);
		echo "<table>";
		echo "<tr><td width='100px'>Server Name<td>".form_input($serverName);
		echo "<tr><td>HostName(IP Address)<td>".form_input($hostName);
		echo "<tr><td>Username<td>".form_input($user);
		echo "<tr><td>Password<td>".form_input($password);
		echo "<tr><td> <br><td>";
		
		echo "<tr><td> <lu><li><a onClick='if(checkForm($(\"addServForm\"))) ";
		
		$homeUrl = "./home";
		$listAllHdUrl = "./home/listAllClientHD";
		
		echo "{new Ajax.Updater($(\"return\"), \"./client/editClientInfo\", {parameters: { clientID:$clientID,name:$(\"serverName\").value, hostName:$(\"hostName\").value, username:$(\"user\").value, password:$(\"password\").value}}); ";
		echo "new Ajax.Updater($(\"leftContainer\"), \"./home/listAllClient\"); }";
		
		echo "'>Edit</a><td><div id='return'></div>";
		echo "</table>";
	}
	
	function addClient()
	{
		if(!$this->session->userdata('login'))
		{
			return;
		}
		$parameters = $_POST['parameters'];
		//echo $parameters;
		
		$pieces = explode(",", $parameters);
		for($i=0; $i<count($pieces); $i++)
		{
			$smPiece = explode(":", $pieces[$i]);
			
			if(isset($smPiece[1]))
			$obj->$smPiece[0] = $smPiece[1];
		}
		
		$serverName = 	$obj->serverName;
		$hostName	=	$obj->hostName;
		$user	=	$obj->user;
		$password	=	$obj->password;
		
		$serverName = trim($serverName);
		$hostName = trim($hostName);
		$user = trim($user);
		$password = trim($password);
		
		echo $serverName."<br>";
		echo $hostName."<br>";
		echo $user."<br>";
		echo $password."<br>";
		
		$this->df602->insert_client_info($serverName, $hostName, $user, $password); 
	}
	
	function addClientForm()
	{
		if(!$this->session->userdata('login'))
		{
			$this->load->view('loginView');
			return;
		}
		
		$serverName = array(
              'name'        => 'serverName',
              'id'          => 'serverName',
              'value'       => '',
              'maxlength'   => '100',
              'size'        => '25',
			  'class'		=> 'require'
            );
			
		$hostName = array(
			  'name'        => 'hostName',
              'id'          => 'hostName',
              'value'       => '',
              'maxlength'   => '100',
              'size'        => '25',
			  'class'		=> 'require'
		);
		
		$user = array(
			  'name'        => 'user',
              'id'          => 'user',
              'value'       => '',
              'maxlength'   => '100',
              'size'        => '25',
			  'class'		=> 'require'
		);
		
		$password = array(
			  'name'        => 'password',
              'id'          => 'password',
              'value'       => '',
              'maxlength'   => '100',
              'size'        => '25',
			  'class'		=> 'require'
		);
		

		
		$attributes = array('id' => 'addServForm', 'onSubmit'=>'return  (checkForm(this))');
		$action = "form/addServer";
		
		echo form_open($action, $attributes);
		echo "<table>";
		echo "<tr><td width='100px'>Server Name<td>".form_input($serverName);
		echo "<tr><td>Hostname(IP Address)<td>".form_input($hostName);
		echo "<tr><td>Username<td>".form_input($user);
		echo "<tr><td>Password<td>".form_input($password);
		echo "<tr><td> <td><br>";
		echo "<tr><td> <lu><li><a onClick='if(submitForm($(\"addServForm\"), \"./client/addClient\")) ";
		
		$homeUrl = "./home";
		$listAllHdUrl = "./home/listAllClientHD";
		
		echo "{new Ajax.Updater($(\"navigate\"), \"./home/rewriteNavigater\", {parameters: { url: \"".$homeUrl."\", name: \"Home\", layer: 0}}); ";
		echo "new Ajax.Updater($(\"leftContainer\"), \"./home/listAllClient\"); ";
		echo "refresher = new Ajax.PeriodicalUpdater($(\"rightContainer\"), \"".$listAllHdUrl."\", {asynchronous:true, frequency:3}); }";
		
		echo "'>Add</a><td>";
		echo "</table>";
		
		echo form_close();
		
		
		
	}

}